package com.itheima.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.itheima.entity.PageResult;
import com.itheima.mapper.RoleMapper;
import com.itheima.mapper.UserMapper;
import com.itheima.pojo.Role;
import com.itheima.pojo.User;
import com.itheima.service.UserService;
import com.itheima.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import tk.mybatis.mapper.entity.Example;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service(interfaceClass = UserService.class)
public class UserServiceImpl implements UserService {

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private RoleMapper roleMapper;

    /*@Autowired
    private BCryptPasswordEncoder bCryptPasswordEncoder;*/

    @Override
    public User findByUsername(String username) {
        return userMapper.findByUsername(username);
    }

    @Override
    public PageResult findPage(Integer currentPage, Integer pageSize, String queryString) {

        PageHelper.startPage(currentPage, pageSize);

        Example example = new Example(User.class);
        Example.Criteria criteria = example.createCriteria();

        if (queryString != null && queryString.trim() != "") {
            criteria.orLike("username", "%" + queryString + "%");
        }

        List<User> list = userMapper.selectByExample(example);

        PageInfo<User> pageInfo = new PageInfo<>(list);

        return new PageResult(pageInfo.getTotal(), pageInfo.getList());
    }

    @Override
    public List<Role> findAll() {
        List<Role> list = roleMapper.selectAll();
        return list;
    }

    @Override
    public void add(Integer[] roleIds, User user) throws ParseException {

//        user.setPassword(bCryptPasswordEncoder.encode(user.getPassword()));

        /*SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        String format = simpleDateFormat.format(user.getBirthday());
        Date parse = simpleDateFormat.parse(format);

        user.setBirthday(parse);*/

        userMapper.insert(user);

        if (roleIds != null && roleIds.length > 0) {
            userMapper.insertUserRole(user.getId(), roleIds);
        }
    }

    @Override
    public Map findAllById(Integer id) {
        User user = userMapper.selectByPrimaryKey(id);
        Integer[] roleIds = userMapper.selectUserRole(id);

        HashMap<Object, Object> map = new HashMap<>();
        map.put("user", user);
        map.put("roleIds", roleIds);

        return map;
    }

    @Override
    public void edit(Integer[] roleIds, User user) {
        userMapper.deleteUserRole(user.getId());

        userMapper.updateByPrimaryKey(user);

        if (roleIds != null && roleIds.length > 0) {
            userMapper.insertUserRole(user.getId(), roleIds);
        }
    }

    @Override
    public void deleteById(Integer id) {
        userMapper.deleteUserRole(id);

        userMapper.deleteByPrimaryKey(id);
    }
}
